package com.ttg.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ttg.contants.TestContants;
import com.ttg.contants.UrlContants;
import com.ttg.pojo.PayPojo;
import com.ttg.pojo.SmsPojo;
import com.ttg.security.EncryptionUtils;
import com.ttg.utils.IdGenerator;
import com.ttg.utils.connection.HttpClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

//~--- classes ----------------------------------------------------------------

/**
 * Class PayDemoController
 * Description
 * Create 2016-06-23 17:32:09
 *
 * @author Ardy
 */
@Controller
@RequestMapping("/pay")
public class PayDemoController {

    /**
     * Field logger
     * Description
     */
    private static final Logger logger = LoggerFactory.getLogger(PayDemoController.class);

    /**
     * Method testPay
     * Description 说明：
     *
     * @param model 说明：
     * @return 返回值说明：
     */
    @RequestMapping
    public String testPay(Model model) {
        //准备页面数据
        SmsPojo smsPojo = new SmsPojo();
        smsPojo.setMerchantId(TestContants.MERCHANT_ID);
        smsPojo.setMerOrderId(IdGenerator.genOrdId16());
        smsPojo.setAppId(TestContants.APP_ID);
        smsPojo.setSignatureAlgorithm(TestContants.SIGNATUREALGORITHM);
        smsPojo.setTransAmt(1.13);
        smsPojo.setCardNo("6221558812340000");
        logger.debug("pay demo:{}",EncryptionUtils.encryption(smsPojo.getAllParameters()));
        smsPojo.setSignatureInfo(EncryptionUtils.encryption(smsPojo.getAllParameters()));
        model.addAttribute("pay", smsPojo);
        return "demo/pay_demo";
    }

    @RequestMapping(value = "/do_pay",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject doPay(PayPojo payPojo){
        payPojo.setMerchantId(TestContants.MERCHANT_ID);
        payPojo.setAppId(TestContants.APP_ID);
        payPojo.setSignatureAlgorithm(TestContants.SIGNATUREALGORITHM);
        payPojo.setSignatureInfo(EncryptionUtils.encryption(payPojo.getAllParameters()));
        logger.debug("do pay :{}", payPojo);
        String resp = HttpClientUtils.sendPostHttpRequest(UrlContants.PAY_URL, "POST", payPojo.getAllParameters());
        return JSON.parseObject(resp);
    }


    @RequestMapping(value = "/do_sms",method = RequestMethod.POST)
    @ResponseBody
    public JSON doSms(String cardNo,String merOrderId,double transAmt){
        SmsPojo smsPojo = new SmsPojo();
        smsPojo.setCardNo(cardNo);
        smsPojo.setMerOrderId(merOrderId);
        smsPojo.setTransAmt(transAmt);
        smsPojo.setMerchantId(TestContants.MERCHANT_ID);
        smsPojo.setAppId(TestContants.APP_ID);
        smsPojo.setSignatureAlgorithm(TestContants.SIGNATUREALGORITHM);
        logger.debug("pay demo:{}",EncryptionUtils.encryption(smsPojo.getAllParameters()));
        smsPojo.setSignatureInfo(EncryptionUtils.encryption(smsPojo.getAllParameters()));
        logger.debug("do sms :{}", smsPojo);
        String resp = HttpClientUtils.sendPostHttpRequest(UrlContants.SMS_URL, "POST", smsPojo.getAllParameters());
        logger.debug("resp :{}",resp);

        return  JSON.parseObject(resp);
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
